/*	HTML5AS3MultiUp v0.1 <http://code.google.com/p/html5-as3-multiupload/> 
	is released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

package flasheep.proj.multiup.as3
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.system.Security;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flasheep.ascaffold.events.DataEvent;
	import flasheep.ascaffold.utils.fileupload.FileRefList;
	import flasheep.proj.multiup.as3.ui.AssetBtn;
	import flasheep.proj.multiup.as3.utils.ListHelper;
	
	/**
	 * ...
	 * @author Luan(tonylua@sina.com)
	 */
	public class Main extends Sprite 
	{
		private var _btn:AssetBtn;
		private var _frList:FileRefList = null;
		private var _setting:Object;
		private var _currentViewIdx:uint = 1;
		private var _list:ListHelper;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			_init();
			_contextmenu();
		}
		
		private function _init():void 
		{
			Security.allowDomain("*");
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;
			
			_setting = this.stage.loaderInfo.parameters;
			
			_onToggleButton(null);
			
			_list = new ListHelper(_setting);
			_list.addEventListener('toggleButton', _onToggleButton);
		}
		
		//右键菜单
		private function _contextmenu():void
		{
			var c:ContextMenu = new ContextMenu();
			c.hideBuiltInItems();
			c.builtInItems.print = false;
			
			var copyright:ContextMenuItem = new ContextMenuItem("HTML5AS3MultiUp v1.0 (" + 1 + ")" );
			copyright.enabled = false;
			c.customItems.push(copyright);
			
			this.contextMenu = c;
		}
		
		//切换按钮
		private function _onToggleButton(evt:DataEvent):void 
		{
			var type:String = (evt == null)
							? _setting.btn_select_cls
							: (evt.data as String);
			
			if (_btn != null) {
				_btn.removeEventListener(MouseEvent.CLICK, _onBtn);
				this.removeChild(_btn);
				_btn = null;
			}
			
			switch(type)
			{
				case _setting.btn_append_cls:
					_btn = new AssetBtn(
						_setting.btn_append_up,
						_setting.btn_append_over
					);
					break;
				case _setting.btn_select_cls:
				default:
					_btn = new AssetBtn(
						_setting.btn_select_up || 'assets/btn_select.jpg', 
						_setting.btn_select_over || 'assets/btn_select_over.jpg'
					);
					break;
			}
			
			_btn.tabEnabled = false;
			_btn.addEventListener(MouseEvent.CLICK , _onBtn);
			this.addChild(_btn);
		}
		
		//点击了"开始上传"
		private function _onBtn(e:MouseEvent):void 
		{
			if (_frList == null)
			{
				_frList = new FileRefList(_setting.url);
				_frList.addEventListener(Event.SELECT, _onFrListSelect);
				_frList.addEventListener(Event.CANCEL, _onFrListCancal);
			}
			_frList.browse([new FileFilter(
				_setting.i18n_fr_filter || '图片文件(jpg,jpeg,gif,png)', 
				_setting.filter || '*.jpg;*.jpeg;*.gif;*.png'
			)]);
		}		
		private function _onFrListCancal(e:Event):void 
		{
			//trace("放弃选择");
		}
		private function _onFrListSelect(e:Event):void 
		{
			_currentViewIdx = 2;
			
			var arr:Array = (e.target as FileRefList).fileList;
			
			//放在列表中展示
			if (_currentViewIdx == 1)	//第一次上传
			{
				_list.fillList(arr);
			}
			else						//继续添加
			{
				_list.concatList(arr);
			}
		}
		
	}
}